package com.itheima.a04polymorphismdemo04;

/***
 *@title Person
 *@description <TODO description class purpose>
 *@author 12468
 *@version 1.0.0
 *@create 2024/10/24 0:31
 **/
public class Person {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    // public void peekPet(Dog dog, String something) {
    //     System.out.println("年龄为" + age + "岁的" + name + "养了一只" + dog.getColor() + "颜色的" + dog.getAge() + "岁的狗");
    //     dog.eat(something);
    // }
    //
    // public void peekPet(Cat cat, String something) {
    //     System.out.println("年龄为" + age + "岁的" + name + "养了一只" + cat.getColor() + "颜色的" + cat.getAge() + "岁的狗");
    //     cat.eat(something);
    // }

    // 优化1
    public void peekPet(Animal animal, String something) {
        // System.out.println("年龄为" + age + "岁的" + name + "养了一只" + animal.getColor() + "颜色的" + animal.getAge() + "岁的动物");
        // animal.eat(something);

        // 优化2
        if(animal instanceof Dog d) {
            System.out.println("年龄为" + age + "岁的" + name + "养了一只" + animal.getColor() + "颜色的" + animal.getAge() + "岁的狗");
            d.eat(something);
        } else if (animal instanceof Cat c) {
            System.out.println("年龄为" + age + "岁的" + name + "养了一只" + animal.getColor() + "颜色的" + animal.getAge() + "岁的猫");
            c.eat(something);
        }
    }
}
